<template>
  <div class="viewcontainer" v-if="dataset">
    <el-row class="view-header">
      <el-col :span="22">
        <hi-lquery v-bind="queryPanel" :target="dataset"></hi-lquery>
      </el-col>
      <el-col :span="2" style="text-align: right">
        <el-button type="primary" @click="showAdd">添加</el-button>
      </el-col>
    </el-row>
    <el-row class="view-main">
      <el-table
        ref="dataTable"
        :data="dataset.data"
        height="100%"
        style="width: 100%"
        v-loading="dataset.loading"
      >
        <el-table-column type="index" width="50" />
        <el-table-column prop="FNUMBER" label="店内编码" width="180" />
        <!-- <el-table-column prop="FBARCODE" label="条形码" width="160" /> -->
        <el-table-column prop="FDESC" label="通用名称" width="180" />
        <el-table-column prop="FCLASS" label="类别" width="180" />
        <el-table-column prop="FSPEC" label="规格" width="300">
          <template slot-scope="scope">
            <div class="text-truncate" :title="scope.row.FSPEC">
              {{ scope.row.FSPEC }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="FUNIT" label="单位" width="140" />
        <el-table-column prop="FPRICE" label="零售价" width="140">
          <template slot-scope="scope">
            {{ formatMoney(scope.row.FPRICE) }}
          </template>
        </el-table-column>
        <el-table-column prop="FPROCOMPANY" label="生产厂家" width="260">
          <template slot-scope="scope">
            <div class="text-truncate" :title="scope.row.FPROCOMPANY">
              {{ scope.row.FPROCOMPANY }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="FMEDICARETYPE"
          label="医保类别"
          width="180"
        />
        <!-- <el-table-column prop="FCREATEDATE" label="创建时间" width="180" /> -->
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showEdit(scope)">
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button type="text" size="small" @click="remove(scope)">
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="view-footer pagebar">
      <hi-pagination
        background
        :total="dataset.total"
        :target="dataset"
      ></hi-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  HiPagination,
  HiLquery,
  DataSetContainer,
  utils,
  dataHelper,
} from "hi-ui";
import viewBase from "../../base";
import drugDatacfg from "./dataset";
export default {
  name: "druglist",
  extends: viewBase,
  components: {
    HiPagination,
    HiLquery,
  },
  data() {
    let datasetCfg = Object.assign({}, drugDatacfg, {
      action: "autoAction",
    });
    return {
      //数据桥梁
      dataBridge: new Map(),
      dataset: null,
      //数据集配置
      datasetCfg: datasetCfg,
      total: 100,
      //查询面板配置
      queryPanel: {
        columnsTotal: 2,
        fields: [
          {
            element: "el-input",
            label: "店内编码",
            name: "FNUMBER",
          },
          // {
          //   element: "el-input",
          //   label: "条形码",
          //   name: "FBARCODE",
          // },
          {
            // element: "el-input",
            label: "通用名称",
            name: "FDESC",
            element: "hi-select-grid",
            props: {
              additionalFields: [
                {
                  name: "FPINYIN",
                  element: "el-input",
                  operate: "like",
                },
                {
                  name: "FJIANPIN",
                  element: "el-input",
                  operate: "like",
                },
              ],
              selectOption: {
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/drug/dataset/DrugEntity.xml",
                // label: "FDESC",
                filtername: "FCOMMON",
                filternames: [
                  {
                    name: "FDESC",
                  },
                  {
                    name: "FPINYIN",
                    // operate: "like",
                  },
                  {
                    name: "FJIANPIN",
                  },
                ],

                active: false,
                tableprops: {
                  // border:true,
                  hideRowBorder: true,
                  stripe: true,
                  "show-header": false,
                  //fit:false,
                  size: "mini",
                  "max-height": 400,
                },
                returnFields: {
                  FDESC: "FDESC",
                  // FSHOPDESCID: "FNUMBER",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "通用名称",
                  },
                ],
              },
            },
          },
        ],
      },
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    formatMoney(val) {
      return utils.number.format(val, "￥");
    },
    showAdd() {
      this.openByPath("/database/drug/detail", null, {
        title: "添加药品",
      });
    },
    showEdit(data) {
      this.openByPath(
        "/database/drug/detail",
        {
          id: data.row.FID,
        },
        {
          title: "【" + data.row.FNUMBER + "】编辑",
          target: "_blank",
        }
      );
    },
    remove(data) {
      this.$confirm("确定要删除【" + data.row.FNUMBER + "】", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let saveHelper = this.createSaveHelper(this.dataset.modelFile);
          saveHelper.remove(data.row);
          saveHelper.save().then((res) => {
            this.dataset.reload();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
          /*   this.dataset.remove(data.row);

                    this.dataset.save().then((res) => {
                        this.$message({
                            type: "success",
                            message: "删除成功!",
                        });
                    }); */
        })
        .catch(() => {});
    },

    // afterquery() {
    //   this.dataset.load();
    // },
  },
  mounted() {
    this.dataset = this.createDataSet(this.datasetCfg);
    /* this.dataset.on("beginload", function (param, store) {
      debugger;
      var val;
      for (var i = 0; i < param.where.items.length; i++) {
        if (param.where.items[i].name == "FDESC") {
          val = param.where.items[i].value;
          param.where.items.splice(i, 1);
          break;
        }
      }
      if (val) {
        let where = new dataHelper.queryHelper.Where({
          criterionData: {
            join: "or",
            items: [
              {
                sign: "like",
                name: "FDESC",
                tablefilter: false,
                dataType: "dbString",
                value: val,
                enabled: true,
              },
              {
                sign: "like",
                name: "FPINYIN",
                tablefilter: false,
                dataType: "dbString",
                value: val,
                enabled: true,
              },
              {
                sign: "like",
                name: "FJIANPIN",
                tablefilter: false,
                dataType: "dbString",
                value: val,
                enabled: true,
              },
            ],
          },
        });
        param.where.addWhere(where);
      }
    }); */

    this.dataBridge.set(this.dataset, ["dataTable"]);
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scope>
</style>
